﻿using System.Windows;
using System.Windows.Controls;

namespace personalplaner.common.controls
{
	[TemplatePart(Name="PART_Presenter",Type=typeof(ItemsPresenter))]
	public class CollapsiblePanel : ItemsControl
	{
        //Button collapseButton;

		static CollapsiblePanel()
        {
			DefaultStyleKeyProperty.OverrideMetadata(typeof(CollapsiblePanel), new FrameworkPropertyMetadata(typeof(CollapsiblePanel)));
        }
        
		#region Properties

		public bool IsCollapsed
		{
			get
			{
				return (bool)GetValue(IsCollapsedProperty);
			}
			set
			{
				SetValue(IsCollapsedProperty, value);
			}
		}

		public static readonly DependencyProperty IsCollapsedProperty =
			DependencyProperty.Register("IsCollapsed", typeof(bool), typeof(CollapsiblePanel), new PropertyMetadata(false));


		private ItemsPresenter Presenter
		{
			get
			{
				return GetTemplateChild("PART_Presenter") as ItemsPresenter;
			}
		}



        public ExpandDirection ExpandDirection
        {
            get
            {
                return (ExpandDirection)GetValue(ExpandDirectionProperty);
            }
            set
            {
                SetValue(ExpandDirectionProperty, value);
            }
        }

        public static readonly DependencyProperty ExpandDirectionProperty =
            DependencyProperty.Register("ExpandDirection", typeof(ExpandDirection), typeof(CollapsiblePanel), new UIPropertyMetadata(ExpandDirection.Left));

        

		#endregion

		#region Overrides

		protected override Size MeasureOverride(Size constraint)
		{
			//foreach (UIElement internalChild in )
			//UIElement child = Content as UIElement;
			//if (child != null)
			//{
			//    child.Measure(constraint);

			//    double width = child.DesiredSize.Width;
			//    double height = child.DesiredSize.Height;

			//    if (height > constraint.Height)
			//        height = constraint.Height;

			//    return new Size(width, height);
			//}

			//return new Size();

			var p = Presenter;
			if (p != null)
				p.Measure(constraint);

			return base.MeasureOverride(constraint);
		}

		protected override Size ArrangeOverride(Size arrangeBounds)
		{
			//UIElement child = Content as UIElement;
			//if (child != null)
			//{

			//    double childWidth = child.DesiredSize.Width;
			//    double childHeight = child.DesiredSize.Height;
			//    //double x = CalculateLeft(childWidth, percent, horizontalReveal);
			//    //double y = CalculateTop(childHeight, percent, verticalReveal);

			//    child.Arrange(new Rect(0.0, 0.0, childWidth, childHeight));

			//    childWidth = child.RenderSize.Width;
			//    childHeight = child.RenderSize.Height;
			//    double width = childWidth;
			//    double height = childHeight;
			//    if (height > arrangeBounds.Height)
			//        height = arrangeBounds.Height;

			//    return new Size(width, height);
			//}

			//return new Size();

			var p = Presenter;
			if (p != null)
				p.Arrange(new Rect(0.0, 0.0, arrangeBounds.Width,arrangeBounds.Height));

			return base.ArrangeOverride(arrangeBounds);
		}

        protected override DependencyObject GetContainerForItemOverride()
		{
			return new CollapsibleItem();
		}

        /// <summary> 
        /// Return true if the item is (or is eligible to be) its own ItemUI
        /// </summary> 
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return (item is CollapsibleItem);
        }

        /// <summary>
        /// Prepares the specified element to display the specified item.
        /// </summary>
        /// <param name="element">The element used to display the specified item.</param>
        /// <param name="item">The item to display.</param>
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            // Add the container to the item containers collection
            var itemContainer = (CollapsibleItem)element;

            if (this.ItemTemplate != null)
            {
                var contentElement = this.ItemTemplate.LoadContent();
                itemContainer.DataContext = item;
                itemContainer.Content = contentElement;
            }
			//else
			//{
			//    itemContainer.Content = item;
			//}
        }

        #endregion
    }
}
